package com.aerfafish.spider.neo4j.repository;

import com.aerfafish.spider.neo4j.dao.FundHoldShare;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author aerfafish
 * @date 2021/4/21 11:52 上午
 */
@Component
public interface FundHoldShareRepository extends Neo4jRepository<FundHoldShare, Long> {

    @Query("merge (fund:Fund{code: $fundCode}) " +
            "merge (share:Share{code: $shareCode})" +
            "merge p=(fund)-[r:FUND_HOLD_SHARE]->(share) " +
            "set r.proportion=$proportion, r.sharesNum=$sharesNum, r.sharesValue=$sharesValue")
    void save(@Param("fundCode") String fundCode,
                             @Param("shareCode") String shareCode,
                             @Param("proportion") Float proportion,
                             @Param("sharesNum") Float sharesNum,
                             @Param("sharesValue") Float sharesValue);

    @Query("match (fund:Fund)-[r:FUND_HOLD_SHARE]->(share:Share) " +
            "where fund.assetSize <> 0 and share.code <> \"\"" +
            "with share.incRate * share.latestValue * -1 / 100 * r.sharesNum / fund.assetSize as rate, fund, r, share " +
            "return fund, r, share order by rate desc limit 20")
    List<FundHoldShare> queryRiskFund();
}
